/*
 * @Author: LVGRAPE
 * @LastEditors: LVGRAPE
 */
#include "uart.h"
#include "rtthread.h"
#include "AnoPTv8FrameFactory.h"
#include "vehicle.h"
// uint16_t rxLen = 0;
uint32_t rxTime = 0;

#define BLE_CMD_HEAD1 0xA5
#define BLE_CMD_HEAD2 0x5A

#define BLE_CMD_TAIL1 0x5A
#define BLE_CMD_TAIL2 0xA5

#define BLE_CMD_LEN 32
void bluetooth_init(void)
{
}
typedef struct ble_cmd_s
{
    uint8_t head1;
    uint8_t head2;
    uint8_t cmd;
    uint8_t len;
    uint8_t data[BLE_CMD_LEN];
    uint8_t check;
} ble_cmd_t;
typedef enum ble_cmd_list
{
    BLE_CMD_FORWARD = 0x01, // thottele,cycle,timeout
    BLE_CMD_BACKWARD = 0x02,
    BLE_CMD_LEFT = 0x03,
    BLE_CMD_RIGHT = 0x04,
    BLE_CMD_BREAK = 0x05,
    BLE_CMD_VEHICLE_START = 0x06,
    BLE_CMD_VEHICLE_STOP = 0x07,

} ble_cmd_list_t;

ble_cmd_t ble_cmd;
static uint8_t bl_cmd_state = 0;
static uint8_t rxLen = 0;
int8_t ble_cmd_parse(ble_cmd_t *b)
{
    rt_kprintf("ble_cmd_parse: cmd:%d\r\n", b->cmd);
    switch (b->cmd)
    {
    case BLE_CMD_FORWARD:
        vehicle_forward();
        break;
    case BLE_CMD_BACKWARD:
        vehicle_backward();
        break;
    case BLE_CMD_LEFT:
        vehicle_left();
        break;
    case BLE_CMD_RIGHT:
        vehicle_right();
        break;
    case BLE_CMD_BREAK:
        vehicle_break();
        break;
    case BLE_CMD_VEHICLE_START:
        vehicle_start();
        break;
    case BLE_CMD_VEHICLE_STOP:
        vehicle_stop();
        break;
    default:
        break;
    }
    return 0;
}
uint8_t ble_cmd_check(ble_cmd_t *b)
{
    // uint8_t check = 0;
    // for (int i = 0; i < b->len; i++)
    // {
    //     check ^= b->data[i];
    // }

    // if (check == b->check)
    // {
    //     return 0;
    // }
    // else
    // {
    //     return check;
    // }
    return 0;
}
int8_t ble_TS_status_get_byte(uint8_t *buff, uint8_t len)
{
    /**
     * @brief
     * TL+00 BLE 处于空状态
     * TL+04 BLE 断开连接
     * TL+01 BLE 处于空闲状态
     * TL+05 BLE 打开监听状态
     * TL+02 BLE 处于广播状态
     * TL+06 BLE 处于扫描状态 -- 主机
     * TL+03 BLE 连接成功
     * TL+07 BLE 搜索完成 -- 主机
     *
     * TS+00 蓝牙还没有连接成功，处于等待配对状态
     * TS+01 蓝牙已经连接成功，但还未播放音乐。空闲
     * TS+02 正在播放音乐
     * TS+03 有电话打出，或者有电话进来 。但是没接听的状态
     * TS+04 正在通话状态，代表已经接通了
     * TS+FF 代表蓝牙音频的资源已经释放，蓝牙没有跑后台，不在蓝牙模式下
     */
    int8_t ts_status = -1;
    if (rt_strncmp((char *)buff, "TS+", 3) == 0)
    {
        ts_status = (buff[3] - '0') * 256 + (buff[4] - '0');
    }
    return ts_status;
}
int8_t ble_cmd_get_byte(uint8_t ch)
{
    switch (bl_cmd_state)
    {
    case 0:
        if (ch == BLE_CMD_HEAD1)
        {
            bl_cmd_state = 1;
        }
        break;
    case 1:
        if (ch == BLE_CMD_HEAD2)
        {
            bl_cmd_state = 2;
        }
        else
        {
            bl_cmd_state = 0;
        }
        break;
    case 2:
        ble_cmd.cmd = ch;
        bl_cmd_state = 0;
        ble_cmd_parse(&ble_cmd);
        break;
    // case 3:
    //     ble_cmd.len = ch;
    //     rxLen = 0;
    //     bl_cmd_state = 0;
    //     break;
    // case 4:
    //     ble_cmd.data[rxLen] = ch;
    //     rxLen++;
    //     if (rxLen == ble_cmd.len || rxLen == BLE_CMD_LEN)
    //     {
    //         bl_cmd_state = 5;
    //         ble_cmd.len = rxLen;
    //     }
    //     break;
    // case 5:
    //     ble_cmd.check = ch;
    //     bl_cmd_state = 0;
    //     if (ble_cmd_check(&ble_cmd) == 0)
    //     {
    //         ble_cmd_parse(&ble_cmd);
    //     }
    //     break;
    default:
        bl_cmd_state = 0;
        break;
    }
    return 0;
}
void bluetooth_routine(void)
{

    while (1)
    {
        int16_t ch = uart1_getChar();
        if (ch != -1)
        {
            // AnoPTv8HwRecvByte(ch);

            // if (rxLen == 0)
            // {
            //     rt_kprintf("Bluetooth: ");
            // }
            rt_kprintf("%02X ", ch);
            // rt_kputs("Bluetooth: ");
            // uart2_put(ch,1);
            ble_cmd_get_byte(ch);
            rxLen++;
            rxTime = rt_tick_get();
        }
        else
        {
            break;
        }
    }

    if (rt_tick_get() > (rxTime + 1000))
    {
        if (rxLen)
        {
            rxLen = 0;
            rt_kprintf("\r\n");
        }
    }
}
